<template>
    <form class="validate-form-container">
        <slot></slot>
        <div
            class="submit-area"
            @click.prevent="submitForm"
        >
            <slot name="submit">
                <button
                    type="submit"
                    class="btn btn-primary"
                >提交</button>
            </slot>
        </div>
    </form>
</template>

<script lang='ts'>
import { defineComponent, onUnmounted } from 'vue'
import mitt from 'mitt'
type ValidateFunc=() => boolean
// 这个定义是让事件和对应的 callback 一一对应
type Events = {'form-item-created': ValidateFunc}
// 实例话 mitt 的时候，传递进去
export const emitter = mitt<Events>()

export default defineComponent({
  emits: ['form-submit'],
  setup (props, context) {
    let funcArr: ValidateFunc[] = []
    const submitForm = () => {
      // 循环执行数组 得到最后的验证结果
      const result = funcArr.map(func => func()).every(result => result)
      context.emit('form-submit', result)
    }
    const callback = (func: ValidateFunc) => {
      funcArr.push(func)
    }
    // 添加监听
    emitter.on('form-item-created', callback)
    onUnmounted(() => {
      // 删除监听
      emitter.off('form-item-created', callback)
      funcArr = []
    })
    return {
      submitForm
    }
  }
})
</script>

<style>
</style>
